#include "disk.h"

#include <iostream>
using std::cout;
using std::endl;

int show_disk_err(std::string msg) {
  cout << "Tip: " << msg << endl;
  auto err = device::Disk::get_err();
  cout << "Code: " << err.no << ", msg: " << err.msg << endl;
  return 0;
}

int show_disk_info(std::string dev) {
  if (dev.empty()) {
    return show_disk_err("No dev input.");
  }

  auto disk = device::Disk::get_disk(dev.c_str());
  if (!disk || !disk->open())
    return show_disk_err(dev);

  cout << "Type: " << disk->get_type_str() << endl;
  cout << "Dev:" << disk->get_dev() << endl;

  cout << "--- base ---" << endl;
  device::Disk::sBaseInfo dsb;
  disk->get_base_info(dsb);

  cout << "Vendor: " << dsb.vendor << endl;
  cout << "Product: " << dsb.product << endl;
  cout << "Sectors: " << dsb.sectors << endl;
  cout << "Sector Size: " << dsb.size / dsb.sectors << endl;
  cout << "All Size (GB): " << dsb.size / 1024 / 1024 / 1024 << endl;


  cout << "--- inquiry ---" << endl;
  device::Disk::sInquiry dsi;
  if (!disk->get_inquiry(dsi))
    return show_disk_err(dev);

  cout << "Vendor: " << dsi.vendor << endl;
  cout << "Product: " << dsi.product << endl;
  cout << "Version: " << dsi.version << endl;
}

int main(int argc, char **argv) {
  if (argc > 1) {
    show_disk_info(argv[1]);
  } else {
    std::set<std::string> disks;
    if (!device::Disk::scan_disk(disks)) {
      return show_disk_err("scan_disk");
    }

    int i = 0;
    for (auto it = disks.begin(); it != disks.end(); ++it, ++i) {
      cout << "--- " << (i + 1) << " ---" << endl;
      show_disk_info(*it);
    }
    cout << "------" << endl;
    cout << "count: " << disks.size() << endl;
  }
  return 0;
}